/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package three2learn.Controller;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
import three2learn.service.HibernateUtil;
import three2learn.Model.Student;

/**
 *
 * @author Robbert
 */
public class StudentWijzigController extends HttpServlet {

    private static String titelNieuw = "Nieuwe Student";
    private static String titelWijzig = "Wijzigen Student";

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        if (request.getParameter("id") != null) {
            long id = Long.parseLong(request.getParameter("id"));
            request.setAttribute("id", id);

            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction transaction = session.beginTransaction();
            Student student = (Student) session.get(Student.class, id);
            transaction.commit();

            request.setAttribute("residence", student.getResidence());
            request.setAttribute("dateofbirth", student.getDob());
            request.setAttribute("title", student.getTitle());
            request.setAttribute("firstname", student.getfName());
            request.setAttribute("lastname", student.getlName());
            request.setAttribute("email", student.geteMail());
            request.setAttribute("passw", student.getPassw());
            request.setAttribute("street", student.getStreet());
            request.setAttribute("homeNumber", student.getHomeNumber());
            request.setAttribute("zipCode", student.getZipCode());

            doorsturen(request, response, titelWijzig);
        } else {
            doorsturen(request, response, titelNieuw);
        }

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction transaction = session.beginTransaction();
        
        Student formStudent = getStudentFromRequest(request);
        boolean isStudentUpdate = request.getParameter("id") != null;
        if (isStudentUpdate) {
            session.update(formStudent);
        } else {
            session.save(formStudent);
        }
        transaction.commit();
        response.sendRedirect("../index.jsp"); // adress nadat de registratie is verzonden
    }

    private void doorsturen(HttpServletRequest request, HttpServletResponse response, String titel)
            throws IOException, ServletException {
        request.setAttribute("paginaTitel", titel);

        String address = "/registerStudent.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }

    private Student getStudentFromRequest(HttpServletRequest request) {
        Student s = new Student();

        if (request.getParameter("id") != null && !request.getParameter("id").isEmpty()) {
            s.setStudentNumber(Long.parseLong(request.getParameter("id")));
        }
        if (request.getParameter("residence") != null) {
            s.setResidence(request.getParameter("residence"));
        }
        if (request.getParameter("dob") != null) {
            s.setDob(request.getParameter("dob")); // moet gekeken worden naar een string to date! tot die tijd is date een string
        }
        if (request.getParameter("title") != null) {
            s.setTitle(request.getParameter("title"));
        }
        if (request.getParameter("fName") != null) {
            s.setfName(request.getParameter("fName"));
        }
        if (request.getParameter("lName") != null) {
            s.setlName(request.getParameter("lName"));
        }
        if (request.getParameter("eMail") != null) {
            s.seteMail(request.getParameter("eMail"));
        }
        if (request.getParameter("passw") != null) {
            s.setPassw(request.getParameter("passw"));
        }
        if (request.getParameter("street") != null) {
            s.setStreet(request.getParameter("street"));
        }
        if (request.getParameter("homeNumber") != null) {
            s.setHomeNumber(request.getParameter("homeNumber"));
        }
        if (request.getParameter("zipCode") != null) {
            s.setZipCode(request.getParameter("zipCode"));
        }
        return s;
    }
}
